package com.example.ViewPagerSum.adapter;


import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.*;


import com.example.ViewPagerSum.PhotoDetailActivity;
import com.example.ViewPagerSum.R;
import com.example.ViewPagerSum.domain.MmPhoto;
import com.example.ViewPagerSum.util.URLTools;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;


public class WaterfallAdapter extends BaseAdapter  {

	private StringBuilder imagePath = new StringBuilder();
	private  ArrayList<MmPhoto> list;
	private Context context;
	private Drawable drawable;
	
	public WaterfallAdapter(ArrayList<MmPhoto> list , Context context) {
		this.list = list;
		this.context = context;
		//先加载一张默认的图片
		drawable = context.getResources().getDrawable(R.drawable.load_default);
	}


	@Override
	public int getCount() {
		return list != null ? list.size() : 0;
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View view, ViewGroup group) {
		final MmPhoto mmPhoto = list.get(position);
		final Holder holder;
		// 得到View
		if (view == null) {
			holder = new Holder();
			LayoutInflater inflater = LayoutInflater.from(context);
			view = inflater.inflate(R.layout.image_item, null);
			holder.ivIcon = (ImageView) view.findViewById(R.id.row_icon);
			holder.tvTitle = (TextView) view.findViewById(R.id.title);

			view.setTag(holder);
		} else {
			holder = (Holder) view.getTag();
		}

		holder.ivIcon.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(context, PhotoDetailActivity.class);
				intent.putExtra("mmPhoto",mmPhoto);
				context.startActivity(intent);
			}
		});
		imagePath.append(URLTools.IMAGRURL).append(mmPhoto.getImg());
		String url = imagePath.toString();
		ImageLoader.getInstance().displayImage(url, holder.ivIcon,
				new ImageLoadingListener() {
					@Override
					public void onLoadingStarted(String imageUri, View view) {
						holder.ivIcon.setImageDrawable(drawable);
						//holder.tvTitle.setVisibility(View.VISIBLE);
						holder.tvTitle.setText(mmPhoto.getTitle());
					}

					@Override
					public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
						String message = null;
						switch (failReason.getType()) {
							case IO_ERROR:
								message = "Input/Output error";
								break;
							case DECODING_ERROR:
								message = "can not be decoding";
								break;
							case NETWORK_DENIED:
								message = "Downloads are denied";
								break;
							case OUT_OF_MEMORY:
								message = "内存不足";
								Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
								break;
							case UNKNOWN:
								message = "Unknown error";
								Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
								break;
						}
						//holder.pbLoad.setVisibility(View.GONE);
					}

					@Override
					public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
						//holder.pbLoad.setVisibility(View.GONE);
					}

					@Override
					public void onLoadingCancelled(String paramString, View paramView) {
					}
				});

		imagePath.delete(0, imagePath.length());
		return view;
	}


	class Holder {
		public ImageView ivIcon;
		public TextView tvTitle;
	}

}

